import dayjs from 'dayjs'
import { BigNumber } from 'bignumber.js'
import {
  showToast as VantToast,
  showNotify as VantNotify,
  showLoadingToast as VantLoading,
} from 'vant'
import 'vant/es/toast/style'
import 'vant/es/notify/style'

// 显示土司
export const showToast = (message = '') => {
  return VantToast(message)
}

// 显示成功信息
export const showSuccess = (message = '') => {
  return VantNotify({ type: 'success', message })
}

// 显示错误信息
export const showError = (message = '') => {
  return VantNotify({ type: 'danger', message })
}

// 显示加载信息
export const showLoading = () => {
  return VantLoading({
    message: '加载中...',
    duration: 60 * 1000,
    forbidClick: true,
  })
}

// 转成BigNumber
export const toBigNumber = (amount = 0) => {
  if (amount === 0) {
    return '0x0000000000000000000000000000000000000000'
  } else {
    return BigNumber(amount)
      .multipliedBy(10 ** 18)
      .toFixed(0)
  }
}

// 转成Number
export const toNumber = (
  amount = '0x0000000000000000000000000000000000000000'
) => {
  return BigNumber(amount)
    .dividedBy(10 ** 18)
    .toFixed(6)
}

// 时间格式化
export function formatDateTime(time = undefined, format = 'YYYY.MM.DD HH:mm') {
  return dayjs(time * 1000).format(format)
}

// 复制文字
export function copyString(str = '') {
  if (typeof str !== 'string') {
    return new Error('参数1 str（字符串）必须为string类型')
  }

  const input = document.createElement('input')

  input.setAttribute('readonly', 'readonly')
  input.setAttribute('value', str)
  input.style.cssText = `position: fixed; top: -200vh; left: -200vw; opacity: 0;`

  document.body.appendChild(input)

  input.select()
  input.setSelectionRange(0, 1000 * 1000)

  if (document.execCommand('copy')) {
    showToast($t('已复制'))
  } else {
    showToast($t('复制失败'))
  }
}
